package zhihui.mobile.ui.welcome;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zgm.utils.DateUtils;
import com.zgm.utils.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.mindpipe.android.logging.log4j.LogConfigurator;
import zhihui.mobile.R;
import zhihui.mobile.app.CrashHandler;
import zhihui.mobile.app.MyApplication;
import zhihui.mobile.config.Config;
import zhihui.mobile.ui.main.MainActivity;

/**
 * Created by 张公铭 on 2017/6/27 0027.
 */

public class GuidanceActivity extends AppCompatActivity
{
    private ViewPager vp_content;
    private Button btn_open;
    private LinearLayout ly_bottom;

    private int[] icons = {R.drawable.guidance_01, R.drawable.guidance_02, R.drawable.guidance_03};
    private List<View> views;

    //底部小圆点
    private ImageView[] dots;

    //当前位置
    private int currentIndex = 0;
    private final int REQUEST_EXTERNAL_STORAGE = 1;
    private String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };
    private LogConfigurator logConfigurator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guidance_activity);
        init();
    }

    private void init()
    {
        vp_content = (ViewPager) findViewById(R.id.vp_guidance_content);
        btn_open = (Button) findViewById(R.id.btn_guidance_open);
        btn_open.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(GuidanceActivity.this, MainActivity.class);
                startActivity(intent);
                MyApplication.finishActivity(GuidanceActivity.this);
            }
        });
        ly_bottom = (LinearLayout) findViewById(R.id.ly_guidace_bottom);

        views = new ArrayList<>();
        for (int i = 0; i < icons.length; i++)
        {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(icons[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            views.add(imageView);
        }
        ViewPagerAdapter adapter = new ViewPagerAdapter(views);
        vp_content.setAdapter(adapter);
        vp_content.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            // 当滑动状态改变时调用
            @Override
            public void onPageScrollStateChanged(int position)
            {
                // arg0 ==1的时辰默示正在滑动，arg0==2的时辰默示滑动完毕了，arg0==0的时辰默示什么都没做。
            }

            // 当前页面被滑动时调用
            @Override
            public void onPageScrolled(int position, float arg1, int arg2)
            {
                // arg0 :当前页面，及你点击滑动的页面
                // arg1:当前页面偏移的百分比
                // arg2:当前页面偏移的像素位置
            }

            // 当新的页面被选中时调用
            @Override
            public void onPageSelected(int position)
            {
                // 设置底部小点选中状态
                setCurDot(position);
            }
        });
        initDots();
        setGuidaceState();
    }

    private void initDots()
    {
        dots = new ImageView[icons.length];
        // 循环取得小点图片
        for (int i = 0; i < icons.length; i++)
        {
            dots[i] = new ImageView(this);
            LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mLayoutParams.gravity = Gravity.CENTER;
            mLayoutParams.leftMargin = 10;
            mLayoutParams.rightMargin = 10;
            dots[i].setLayoutParams(mLayoutParams);
            dots[i].setImageResource(R.drawable.guidance_icon_bk);
            dots[i].setEnabled(false);// 都设为灰色
            dots[i].setTag(i);// 设置位置tag，方便取出与当前位置对应
            ly_bottom.addView(dots[i]);
        }
        currentIndex = 0;
        dots[currentIndex].setEnabled(true); // 设置为白色，即选中状态
    }

    /**
     * 设置当前指示点
     *
     * @param position
     */
    private void setCurDot(int position)
    {
        if (position < 0 || position > icons.length || currentIndex == position)
        {
            return;
        }
        dots[position].setEnabled(true);
        dots[currentIndex].setEnabled(false);
        currentIndex = position;
    }

    private void setGuidaceState()
    {
        SharedPreferences preferences = this.getSharedPreferences(Config.GUIDANCE_STATE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(Config.GUIDANCE_STATE_KEY, false);
        editor.commit();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    class ViewPagerAdapter extends PagerAdapter
    {
        private List<View> list;

        public ViewPagerAdapter(List<View> list)
        {
            this.list = list;
        }

        @Override
        public int getCount()
        {
            if (list == null)
            {
                return 0;
            }
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object)
        {
            return (view == object);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2)
        {
            if (arg1 >= getCount())
            {
                return;
            }
            ((ViewPager) arg0).removeView(list.get(arg1));
        }

        @Override
        public Object instantiateItem(View arg0, int arg1)
        {
            ((ViewPager) arg0).addView(list.get(arg1));
            return list.get(arg1);
        }
    }
}
